/*
 * Copyright (c) 2019.11，华乘电气科技有限公司
 * All rights reserved.
 *
 * 文件名称：binaryprocesshelper.h
 * 
 * 初始版本：1.0
 * 作者：洪澄
 * 创建日期：2019/11/07
 * 摘要：二进制处理助手
 * 当前版本：1.0
 */

#pragma once

#include <QDataStream>
#include "dataspecification_def.h"
#include <vector>
namespace DataSpecificationGW
{
    template<typename T>
    bool parseBinary(QDataStream& in, T& field)
    {
        in >> field;
        return true;
    }

    template<typename T>
    void parseBinaryField(QDataStream& in, const bool bSkip, T& field, bool& bFieldExist)
    {
        bFieldExist = false;
        if (bSkip)
        {
            return;
        }

        if (parseBinary(in, field))
        {
            bFieldExist = true;
        }
    }

    template<typename T>
    void parseBinaryField(QDataStream& in, const bool bSkip, std::vector<T>& field, const size_t size, bool& bFieldExist)
    {
        bFieldExist = false;
        if (bSkip)
        {
            return;
        }

        field.reserve(size);
        for (size_t i = 0; i < size; ++i)
        {
            T tmpValue;
            in >> tmpValue;
            field.push_back(tmpValue);
        }

        bFieldExist = true;
    }

    void parseBinaryField(QDataStream& in, const bool bSkip, const DataSpecificationGW::StringType eStringType, const size_t size, QString& qstrField, bool& bFieldExist);
}
